package com.clei.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * BigDecimal 工具类
 *
 * @author KIyA
 */
public class BigDecimalUtil {

    /**
     * 100
     */
    private final static BigDecimal ONE_HUNDRED = new BigDecimal(100);

    /**
     * 获得n位小数的float
     *
     * @param f float
     * @param n scale
     * @return float
     */
    public static float scaleFloat(float f, int n) {
        BigDecimal bigDecimal = new BigDecimal(f).setScale(n, RoundingMode.HALF_UP);
        return bigDecimal.floatValue();
    }

    /**
     * 获得n位小数的float
     *
     * @param b BigDecimal
     * @param n scale
     * @return float
     */
    public static float scaleFloat(BigDecimal b, int n) {
        return b.setScale(n, RoundingMode.HALF_UP).floatValue();
    }

    /**
     * 去掉字符串小数尾巴上的0
     *
     * @param str String float
     * @return String float
     */
    public static String trimTailZeros(String str) {
        int dotIndex = str.indexOf(".");
        if (-1 != dotIndex) {
            int length = str.length();
            while (length != dotIndex) {
                if ('0' != str.charAt(--length)) {
                    break;
                }
            }
            if (length == dotIndex) {
                return str.substring(0, dotIndex);
            }
            return str.substring(0, length + 1);
        }
        return str;
    }

    /**
     * scaled
     *
     * @param number 数
     * @return String
     */
    public static String scaled(String number) {
        return scaled(number, 2);
    }

    /**
     * scaled
     *
     * @param number 数
     * @param scale  保留小数位
     * @return String
     */
    public static String scaled(String number, int scale) {
        if (StringUtil.isBlank(number)) {
            return "0";
        }
        return new BigDecimal(number).setScale(scale, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
    }

    /**
     * scaled
     *
     * @param bigDecimal 数
     * @return String
     */
    public static String scaled(BigDecimal bigDecimal) {
        if (null == bigDecimal) {
            return "0";
        }
        return bigDecimal.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
    }

    /**
     * scaledRate
     *
     * @param dividend 除数
     * @param divisor  被除数
     * @return String
     */
    public static String scaledRate(int dividend, int divisor) {
        return scaledRate(dividend, divisor, 2);
    }

    /**
     * scaledRate
     *
     * @param dividend 除数
     * @param divisor  被除数
     * @param scale    保留小数位
     * @return String
     */
    public static String scaledRate(int dividend, int divisor, int scale) {
        if (0 == dividend || 0 == divisor) {
            return "0";
        }
        if (dividend >= divisor) {
            return "100";
        }
        return new BigDecimal(dividend * 100).divide(new BigDecimal(divisor), scale, RoundingMode.HALF_UP)
                .stripTrailingZeros().toPlainString();
    }

    /**
     * scaledRate
     *
     * @param dividend 除数
     * @param divisor  被除数
     * @return String
     */
    public static String scaledRate(String dividend, String divisor) {
        return scaledRate(dividend, divisor, 2);
    }

    /**
     * scaledRate
     *
     * @param dividend 除数
     * @param divisor  被除数
     * @param scale    保留小数位
     * @return String
     */
    public static String scaledRate(String dividend, String divisor, int scale) {
        if (StringUtil.isBlank(dividend) || StringUtil.isBlank(divisor)
                || "0".equals(dividend) || "0".equals(divisor)) {
            return "0";
        }
        if (dividend.compareTo(divisor) > 0) {
            return "100";
        }
        return new BigDecimal(dividend).multiply(ONE_HUNDRED).divide(new BigDecimal(divisor), scale, RoundingMode.HALF_UP)
                .stripTrailingZeros().toPlainString();
    }

    /**
     * scaledRate
     *
     * @param dividend 除数
     * @param divisor  被除数
     * @return String
     */
    public static String scaledRate(BigDecimal dividend, BigDecimal divisor) {
        if (null == dividend || null == divisor || 0 == BigDecimal.ZERO.compareTo(dividend)
                || 0 == BigDecimal.ZERO.compareTo(divisor)) {
            return "0";
        }
        if (dividend.compareTo(divisor) > 0) {
            return "100";
        }
        return dividend.multiply(ONE_HUNDRED).divide(divisor, 2, RoundingMode.HALF_UP)
                .stripTrailingZeros().toPlainString();
    }
}
